{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset238{\*\fname Arial;}Arial CE;}{\f1\fswiss\fcharset0 Arial;}{\f2\fswiss\fcharset163 Arial;}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20 BufferedImage bufI = null;\par
try \{\par
bufI = ImageIO.read(new File(imgPath));\par
//C la 1 c\'e1i component n\f1\'e0o \f0\'f0\'f3,1 panel ch\u7859?ng h\u7841?n\par
Graphics2d g=(Graphics2D) c.getGraphics();\par
g.drawImage(bufImg, null, 0,0);\par
\} catch (Exception e) \{\par
e.printStackTrace();\par
\}\par
\f1 '''''''''''''''''''''''''''''''''''''''''''''''''''\f0 public class ShowImage extends javax.swing.JFrame \par
 \{\par
 BufferedImage bufImg  ;\par
 File file = new File("screen.png");\par
 Image img;\par
 public ShowImage() throws IOException \{\par
         initComponents();\par
         bufImg = ImageIO.read(file);\par
         img = Toolkit.getDefaultToolkit().getImage("screen.png");\par
         img = img.getScaledInstance(jPanel1.getWidth(), jPanel1.getHeight(), Image.SCALE_FAST);        \par
 \par
     \}\par
 \par
     public void paintComponent()\par
     \{\par
         Graphics g = jPanel1.getGraphics();\par
         g.drawImage(img, 0 , 0, jPanel1.getWidth(),jPanel1.getHeight(),jPanel1);\par
     \}\par
 \}\par
 \par
\f1 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\f0\par
public class ShowImage extends javax.swing.JFrame \par
 \{\par
 BufferedImage bufImg  ;\par
 File file = new File("screen.png");\par
 Image img;\par
 public ShowImage() throws IOException \{\par
         initComponents();\par
         bufImg = ImageIO.read(file);\par
         img = Toolkit.getDefaultToolkit().getImage("screen.png");\par
         img = img.getScaledInstance(jPanel1.getWidth(), jPanel1.getHeight(), Image.SCALE_FAST);        \par
 \par
     \}\par
 \par
     public void paintComponent()\par
     \{\par
         Graphics g = jPanel1.getGraphics();\par
         g.drawImage(img, 0 , 0, jPanel1.getWidth(),jPanel1.getHeight(),jPanel1);\par
     \}\par
 \}\par
 \par
\f1 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\f0\par
\par
\'d0\'e2y l\f1\'e0 source code ch\f2\'fd\'f5ng tr\f1\'ecnh xem v\'e0 x\u7917? l\'fd \u7843?nh c\u7911?a tui. V\'ec ch\u7881? l\'e0 minh h\u7885?a th\'f4i n\'ean ch\u7881? c\'f3 ch\u7913?c n\f0\'e3ng load \u7843?nh l\f1\'ean, ph\'f3ng to \u7843?nh m\'e0 th\'f4i.\par
\par
file ImageViewer.java\par
package imageview;\par
\par
import java.awt.*;\par
import java.awt.event.*;\par
import java.awt.image.*;\par
import java.io.*;\par
\par
import javax.imageio.ImageIO;\par
import javax.swing.*;\par
\par
public class ImageViewer extends JFrame implements ActionListener \{\par
BufferedImage image = null;\par
JScrollPane s = new JScrollPane();\par
JMenu file, view;\par
JMenuItem open, exit, zoomin;\par
\par
JPanel pnlImage = new JPanel()\{\par
public void paintComponent(Graphics g)\{\par
super.paintComponent(g);\par
if(image != null)\par
g.drawImage(image, 0, 0, null);\par
\}\par
\};\par
\par
public ImageViewer()\{\par
initMenu();\par
s.add(pnlImage);\par
s.setViewportView(pnlImage);\par
this.getContentPane().add(s);\par
setTitle("Chuong trinh xu ly anh");\par
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );\par
this.setSize(Toolkit.getDefaultToolkit().getScreen Size());\par
\par
\}\par
public void initMenu()\{\par
JMenuBar mbar = new JMenuBar();\par
file = new JMenu("File");\par
view = new JMenu("View");\par
mbar.add(file);\par
mbar.add(view);\par
\par
open = new JMenuItem("Open");\par
file.add(open);\par
open.addActionListener(this);\par
\par
exit = new JMenuItem("Exit");\par
file.add(exit);\par
exit.addActionListener(this);\par
\par
zoomin = new JMenuItem("Zoom in");\par
view.add(zoomin);\par
zoomin.addActionListener(this);\par
\par
this.setJMenuBar(mbar);\par
\}\par
\par
public static void main(String[] args) \{\par
JFrame frame = new ImageViewer();\par
frame.show();\par
\par
\}\par
public void actionPerformed(ActionEvent e) \{\par
if(e.getSource() == open)\{\par
File f = new File("C:\\\\Anh_0001.jpg");\par
try \{\par
image= ImageIO.read(f);\par
System.out.println(f.toString());\par
\} catch (IOException e1) \{\par
System.out.println("Cound't find " + f);\par
\}\par
\}\par
else if(e.getSource() == exit)\{\par
System.exit(0);\par
\}\par
else if(e.getSource() == zoomin)\{\par
System.out.println(image.getHeight() +" " + image.getWidth());\par
image = zoomIn(image, 2);\par
System.out.println(image.getHeight() +" " + image.getWidth());\par
repaint();\par
\}\par
\par
\}\par
public BufferedImage zoomIn(BufferedImage bi, int scale)\par
\{\par
int width = scale * bi.getWidth();\par
int height = scale * bi.getHeight();\par
\par
BufferedImage biScale = new BufferedImage(width, height, bi.getType());\par
\par
for(int i=0; i<width; i++)\par
for(int j=0; j<height; j++)\par
biScale.setRGB(i, j, bi.getRGB(i/scale, j/scale));\par
\par
return biScale;\par
\}\par
\}//end of class\par
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\par
\par
package com.thuvienkhoahoc.wordtomwtext.data;\par
\par
import java.awt.image.BufferedImage;\par
import java.io.File;\par
import java.io.IOException;\par
\par
import javax.imageio.ImageIO;\par
\par
import net.sourceforge.jwbf.contentRep.mw.SimpleFile;\par
\par
public class Image extends SimpleFile \{\par
\par
        private Project project;\par
        private boolean markedForRemoval;\par
        private BufferedImage bufferedImage = null;\par
        /*\par
         * Thay th\u7871? tr\f2\'fd\u7901?ng file c\u7911?a SimpleFile do n\'f3 k cho ph\'e9p\par
         * thay \'f0\u7893?i gi\'e1 tr\u7883? c\u7911?a tr\'fd\u7901?ng n\'e0y.\par
         * XXX s\u7917?a l\u7841?i SimpleFile\par
         */\par
        private File file;\par
\par
        public Image(String label, String filename) \{\par
                super(label, filename);\par
                file = super.getFile();\par
        \}\par
\par
        public Project getProject() \{\par
                return project;\par
        \}\par
\par
        /*\par
         * \'d0\'fd\u7907?c g\u7885?i t\u7915? h\'e0m Project.addImage, c\'e1c thao t\'e1c ki\u7875?m tra \'f0\f1\'e3\par
         * th\u7921?c hi\u7879?n xong.\par
         */\par
        void setProject(Project project) \{\par
                this.project = project;\par
        \}\par
\par
        public boolean isMarkedForRemoval() \{\par
                return markedForRemoval;\par
        \}\par
\par
        public void setMarkedForRemoval(boolean markedForRemoval) \{\par
                if (this.markedForRemoval != markedForRemoval) \{\par
                        boolean oldValue = this.markedForRemoval;\par
                        this.markedForRemoval = markedForRemoval;\par
                        if (project != null) \{\par
                                project.fireImagePropertyChanged(this, oldValue,\par
                                                markedForRemoval, "markedForRemoval");\par
                        \}\par
                \}\par
        \}\par
\par
        public BufferedImage getBufferedImage() throws IOException \{\par
                if (bufferedImage == null) \{\par
                        bufferedImage = ImageIO.read(getFile());\par
                \}\par
                return bufferedImage;\par
        \}\par
\par
        @Override\par
        public void setLabel(String newLabel) \{\par
                if (newLabel.equals(getLabel())) \{\par
                        return;\par
                \}\par
                if (project != null) \{\par
                        project.checkImageTitle(newLabel, this);\par
                \}\par
                String oldLabel = getLabel();\par
                super.setLabel(newLabel);\par
                if (project != null) \{\par
                        project.fireImagePropertyChanged(this, oldLabel, newLabel, "label");\par
                \}\par
        \}\par
       \par
        public void setLabelAndRefactor(String newLabel) \{\par
                String oldLabel = getLabel();\par
                setLabel(newLabel);\par
                project.refactorImageRenamed(oldLabel, newLabel);\par
        \}\par
\par
        @Override\par
        public String toString() \{\par
                return getLabel();\par
        \}\par
       \par
        @Override\par
        public File getFile() \{\par
                return file;\par
        \}\par
\par
        @Override\par
        public String getFilename() \{\par
                return file.getPath();\par
        \}\par
       \par
        public void setFile(File file) \{\par
                this.file = file;\par
                bufferedImage = null;\par
        \}\par
       \par
\}\par
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\par
JAVA t\u7841?o h\'ecnh thu nh\u7887? JPG\par
\par
\par
Trong b\u7845?t k\u7923? trang web to\'e0n di\u7879?n, ch\'fang ta th\f2\'fd\u7901?ng c\u7847?n t\u7843?i l\'ean m\u7897?t s\u7889? th\'f4ng tin h\f1\'ecnh \u7843?nh. Nh\f2\'fdng v\u7899?i s\u7921? ph\u7893? bi\u7871?n c\u7911?a \'f0\u7897? ph\'e2n gi\u7843?i cao DC, t\u7843?i l\'ean c\'e1c h\f1\'ecnh \u7843?nh s\u7869? c\'f3 m\u7897?t dung l\f2\'fd\u7907?ng \'f0\'e1ng k\u7875?, nh\'fd 3.000.000 t\'e0i li\u7879?u ra kh\u7887?i DC pixel c\'f5 b\u7843?n kh\'f4ng d\'fd\u7899?i 600K. \'d0\u7889?i v\u7899?i qu\u7843?n l\f1\'fd d\u7877? d\'e0ng, ch\'fang t\'f4i c\'f3 th\u7875? mi\u7877?n c\f2\'fd\u7905?ng s\u7917? d\u7909?ng ACDSee \'f0\u7875? s\u7917?a \'f0\u7893?i n\'f3 m\u7895?i th\u7901?i gian, v\'e0 tr\u7921?c ti\u7871?p t\u7843?i l\'ean m\'e1y ch\u7911?. Nh\'fdng ph\'fd\'f5ng ph\'e1p ti\u7871?p c\u7853?n kh\'e1ch h\'e0ng n\'e0y c\'f3 v\u7867? kh\'f4ng d\u7877? d\'e0ng, v\'e0 cho ng\'fd\u7901?i s\u7917? d\u7909?ng Internet dial-up \'f0\'f5n gi\u7843?n ch\u7881? l\'e0 m\u7897?t c\'f5n \'e1c m\u7897?ng, m\u7863?c d\'f9 b\u7841?n c\'f3 th\u7875? \'f0\u7863?t trong khu v\u7921?c \u7843?nh r\u7897?ng v\'e0 cao!\par
\par
V\u7845?n \'f0\u7873? gi\u7843?i ph\'e1p l\'e0 \u7903? \'f0\'e2y! Ch\'fang t\'f4i c\'f3 th\u7875? x\u7917? l\f1\'fd m\u7897?t l\u7899?p h\u7885?c l\u7899?n h\f2\'f5n, v\'e0 gi\u7843?m n\'f3.\par
Premised tr\'ean JDK1.4 c\u7847?n, \'f0\u7875? \'f0\'fd\u7907?c x\u7917? l\f1\'fd. Th\u7921?c hi\u7879?n b\u7903?i c\'e1c ph\f2\'fd\'f5ng ph\'e1p sau \'f0\'e2y:\par
\par
java.io.File nh\u7853?p kh\u7849?u;\par
java.io.FileOutputStream nh\u7853?p kh\u7849?u;\par
java.awt.Graphics nh\u7853?p kh\u7849?u;\par
java.awt.Image nh\u7853?p kh\u7849?u;\par
java.awt.image.BufferedImage nh\u7853?p kh\u7849?u;\par
\par
com.sun.image.codec.jpeg.JPEGCodec nh\u7853?p kh\u7849?u;\par
com.sun.image.codec.jpeg.JPEGImageEncoder nh\u7853?p kh\u7849?u;\par
\par
public class JpgTest (\par
\par
public void JpgTset () throws Exception (\par
File _file = new File ("/ Order005-0001.jpg"); / / \'f0\u7885?c file\par
Image src = javax.imageio.ImageIO.read (_file); / / x\'e2y d\u7921?ng \'f0\u7889?i t\'fd\u7907?ng Image\par
int wideth = src.getWidth (null); / / c\'f3 \'f0\'fd\u7907?c chi\u7873?u r\u7897?ng \u7843?nh ngu\u7891?n\par
int chi\u7873?u cao = src.getHeight (null); / / nh\u7853?n h\f1\'ecnh \u7843?nh chi\u7873?u d\'e0i ngu\u7891?n\par
BufferedImage tag = new BufferedImage (wideth / 2, chi\u7873?u cao / 2, BufferedImage.TYPE_INT_RGB);\par
tag.getGraphics () drawImage (src, 0,0, wideth / 2, chi\u7873?u cao / 2, null).; / / v\u7869? c\'e1c b\u7843?n \f0\'f0\u7891? sau khi thu h\u7865?p\par
FileOutputStream ra = new FileOutputStream ("newfile.jpg"); / / xu\u7845?t ra m\u7897?t d\f1\'f2ng su\u7889?i file\par
JPEGImageEncoder b\u7897? m\'e3 h\'f3a = JPEGCodec.createJPEGEncoder (trong);\par
encoder.encode (tag); / / g\u7847?n JPEG m\'e3 h\'f3a\par
/ / System.out.print (chi\u7873?u r\u7897?ng +"*"+ chi\u7873?u cao);\par
out.close ();\par
)\par
)\par
\par
Qu\'e1 tr\'ecnh n\'e0y \f0\'f0\u417?n gi\u7843?n, \'f0\u7885?c t\u7915? \'f0\u7883?a ph\u432?\u417?ng \'f0\u297?a file Order005-0001.jpg (2032 * 1524), v\f1\'e0o Image \f0\'f0\u7889?i t\u432?\u7907?ng src, v\f1\'e0 sau \f0\'f0\'f3 x\'e2y d\u7921?ng c\'e1c t\u7915? kh\'f3a m\u7909?c ti\f1\'eau t\u7853?p tin, thi\u7871?t l\u7853?p \f0\'f0\u7897? d\f1\'e0i c\u7911?a t\u7915? kh\'f3a v\'e0 chi\u7873?u r\u7897?ng c\u7911?a m\u7897?t n\u7917?a c\u7911?a b\u7843?n \f0\'f0\u7891? ngu\u7891?n, \'f0\u432?\u7907?c m\f1\'e3 h\'f3a tr\'ean c\'e1c t\u7915? kh\'f3a, s\u7843?n l\f2\'fd\u7907?ng \'f0\u7875? c\'e1c d\f1\'f2ng file ra, \f0\'f0\'f3ng m\u7899?i c\'e1c d\f1\'f2ng t\u7853?p tin.\par
\par
C\'f3 m\u7897?t s\u7889? b\'e1o tr\f2\'fd\u7899?c:\par
Th\u7913? nh\u7845?t, hi\u7879?n nay ch\u7881? h\u7895? tr\u7907? JPG (JPEG), GIF, PNG ba \'f0\u7883?nh d\u7841?ng.\par
Th\u7913? hai, n\'e3ng l\u7921?c c\u7911?a b\u7843?n \'f0\u7891? ngu\u7891?n b\u7883? h\u7841?n ch\u7871?, t\u7889?t nh\u7845?t kh\'f4ng qu\'e1 1M, n\u7871?u kh\'f4ng h\u7885? s\u7869? n\'e9m ra kh\u7887?i b\u7897? nh\u7899? l\u7895?i, nh\'fdng t\'f4i \'f0\f1\'e3 c\u7889? g\u7855?ng 1.8m ngu\u7891?n b\u7843?n \f0\'f0\u7891?, b\u7841?n c\'f3 th\u7875? th\f1\'e0nh c\'f4ng, nh\f2\'fdng c\u361?ng r\u7845?t d\u7877? d\'e0ng \'f0\u7875? n\'e9m b\u7897? nh\u7899?.\par
\par
Tr\'edch d\u7851?n l\u7901?i c\u7911?a m\u7897?t ng\'fd\u7901?i ti\u7873?n nhi\u7879?m: h\f1\'ecnh \u7843?nh ch\'ednh l\'e0 t\'ednh to\'e1n chuy\'ean s\'e2u m\'e1y t\'ednh, b\u7897? nh\u7899? l\f2\'fdu tr\u7919? l\u7899?n c\u7911?a gi\'e1 tr\u7883? pixel. T\'f4i \'f0\f1\'e3 th\u7917? n\'f3 v\u7899?i VC, 4M h\'ecnh \u7843?nh c\'f3 v\u7845?n \f0\'f0\u7873?, nh\u432?ng n\'e9n nhi\u7873?u h\u417?n nh\u7919?ng b\u7913?c tranh trong b\u7897? nh\u7899? bitmap kh\'f4i ph\u7909?c l\u7841?i b\u7897? nh\u7899? khi l\u7899?n h\u417?n. C\'e1c gi\u7843?i ph\'e1p, b\u7841?n c\'f3 th\u7875? vi\u7871?t l\u7841?i m\f1\'e3 l\u7899?p, \f0\'f0\u7847?u ti\f1\'ean m\u7903? m\u7897?t b\u7897? nh\u7899? nh\u7845?t \f0\'f0\u7883?nh, v\f1\'e0 sau \f0\'f0\'f3 m\u7897?t ph\u7847?n c\u7911?a m\f1\'e3 v\f0\'e3n b\u7843?n \'f0\u7871?n t\u7853?p tin t\u7841?m th\u7901?i, ph\u7847?n \'f0\u7847?u ra, khi \'f0\u7885?c to m\u7897?t l\u7847?n n\u7919?a. Ho\u7863?c s\u7917? d\u7909?ng b\u7897? nh\u7899? nio h\f1\'ecnh \u7843?nh \f0\'f0\u7875? ho\u7841?t \'f0\u7897?ng. JavaMail nh\u432? s\u7917? d\u7909?ng Builder pattern, th\u432?a ng\f1\'e0i nh\f2\'fd l\'e0 m\u7897?t ph\u7847?n c\u7911?a m\u7895?i tin nh\u7855?n, v\'e0 sau \'f0\'f3 k\u7871?t h\u7907?p th\'e0nh m\u7897?t \'f0\u7889?i t\'fd\u7907?ng m\u7897?t ho\'e0n mail, sao cho m\u7895?i th\'e0nh ph\u7847?n \'f4ng Duyao Cheng v\'e0o b\u7897? nh\u7899?, b\u7841?n n\u7871?u b\u7841?n g\u7917?i m\u7897?t megahertz Shang c\u7911?a t\u7853?p tin \'f0\'ednh k\'e8m, T\'ean Zaigou Zao B\'e1n th\u7901?i gian ch\u7855?c ch\u7855?n c\u7911?a b\u7897? nh\u7899?, v\f1\'ec v\u7853?y t\'f4i c\'f3 vi\u7871?t l\u7841?i c\u7911?a BodyPart c\f2\'f5 c\u7845?u, do \'f0\'f3, r\u7857?ng \'f4ng v\'e0 m\u7897?t t\u7853?p tin li\'ean k\u7871?t t\u7841?m th\u7901?i, sau \'f0\'f3 m\u7897?t ph\u7847?n t\u7853?p tin t\u7841?m th\u7901?i h\'f5n l\'e0 ki\u7871?n t\u7841?o trong b\u7897? nh\u7899? c\u7911?a, do \'f0\'f3, k\'e8m theo b\u7845?t k\u7923? \f1\'fd ngh\u297?a Daxiao (Ying Panneng \f0\'f0\u7863?t n\'f3 xu\u7889?ng \'f0\u7875? gi\u7899?i h\u7841?n ) c\'f3 th\u7875? \'f0\u432?\u7907?c g\u7917?i \'f0i.\par
\par
Cu\u7889?i c\f1\'f9ng, n\u7871?u ng\f2\'fd\u7901?i d\'e2n c\'f3 y\'eau c\u7847?u cao h\'f5n cho x\u7917? l\f1\'fd h\'ecnh \u7843?nh, c\'f3 th\u7875? mu\u7889?n xem x\'e9t c\'e1c d\u7921? \'e1n m\'e3 ngu\u7891?n m\u7903?. V\'ed d\u7909? JMagick, b\u7841?n c\'f3 th\u7875? s\u7917? d\u7909?ng \f0\'f0\u7875? \'f0\u7841?t \'f0\u432?\u7907?c h\f1\'ecnh \u7843?nh JMagick nh\'e2n r\u7897?ng, mua l\u7841?i th\'f4ng tin, g\'f3c \f0\'f0\u7897?, c\'e1c hi\u7879?u \u7913?ng \'f0\u7863?c bi\u7879?t, k\u7871?t h\u7907?p \'f0\u7875? thay \'f0\u7893?i k\'edch th\u432?\u7899?c, th\f1\'eam vi\u7873?n, xoay, c\u7855?t nh\u7887?, thay \f0\'f0\u7893?i \'f0\u7883?nh d\u7841?ng cho \'f0\u7871?n m\f1\'e0u s\u7855?c v\'e0 nhi\u7873?u h\f2\'f5n n\u7919?a.\par
\f1 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\par
c\'e1c b\u7841?n g\'fap tui t\'ecm ra l\u7895?i trong ch\f2\'fd\'f5ng tr\f1\'ecnh xem \u7843?nh n\'e0y \f0\'f0c ko v\u7853?y?\par
Ch\u432?\u417?ng tr\f1\'ecnh c\u7911?a tui g\u7891?m 2 ph\u7847?n: Ph\u7847?n ControlPanel ch\u7913?a c\'e1c button - t\u7841?m th\u7901?i \u7903? \f0\'f0\'e2y ch\u7881? c\'f3 2 button l\f1\'e0 LoadImage v\'e0 Zoom th\'f4i - v\'e0 ph\u7847?n ImagePanel ch\u7913?a \u7843?nh.\par
Tui load \u7843?nh l\'ean \f0\'f0c r\u7891?i, gi\u7901? mu\u7889?n Zoom \u7843?nh l\f1\'ean. Tuy nhi\'ean ko bi\u7871?t do sai ch\u7895? n\'e0o m\'e0 khi nh\u7845?n Zoom th\'ec \u7843?nh ko thay \f0\'f0\u7893?i g\f1\'ec c\u7843?. Mong c\'e1c b\u7841?n ch\u7881? gi\'fap.\par
Sau \f0\'f0\'e2y l\f1\'e0 source code c\u7911?a ch\f2\'fd\'f5ng tr\f1\'ecnh, g\u7891?m 3 class: MainSolution, ImagePanel v\'e0 ControlPanel:\par
\par
class MainSolution\par
\par
import java.awt.*;\par
\par
import javax.swing.*;\par
import javax.swing.border.EtchedBorder;\par
\par
public class MainSolution extends JFrame\{\par
private static final long serialVersionUID = 1L;\par
\par
ControlPanel control;\par
ImagePanel ip;\par
\par
\par
/** Constructor */\par
public MainSolution()\{\par
super("Chuong trinh nhan dang phieu thi trac nghiem");\par
Container container = getContentPane();\par
// get the default look of the current computer:\par
try\{\par
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());\par
SwingUtilities.updateComponentTreeUI(this);\par
\} catch(Exception e)\{\par
throw new RuntimeException("Could not set default look and feel");\par
\}\par
\par
\par
ip = new ImagePanel(this);\par
ip.setBorder(new EtchedBorder());\par
JScrollPane sp = new JScrollPane(ip);\par
container.add(sp, BorderLayout.CENTER);\par
\par
control = new ControlPanel(this);\par
control.setBorder(new EtchedBorder());\par
container.add(control, BorderLayout.NORTH);\par
\par
\}\par
\par
public ImagePanel getImagePanel()\{\par
return ip;\par
\}\par
\par
\par
/** Set the default file name for the browse box */\par
public void setDefaultFilename(String name)\{\par
control.setDefaultFilename(name);\par
\}\par
\par
/** Start program: */\par
@SuppressWarnings("deprecation")\par
public static void main(String[] arg)\{\par
MainSolution main = new MainSolution();\par
main.setSize(Toolkit.getDefaultToolkit().getScreen Size());\par
main.setDefaultCloseOperation(JFrame.DISPOSE_ON_CL OSE);\par
main.show();\par
\}\par
\}\par
\par
\par
\par
class ControlPanel\par
\par
import java.awt.*;\par
import java.io.*;\par
\par
import javax.imageio.ImageIO;\par
import javax.*;\par
\par
\par
public class ControlPanel extends JPanel \{\par
\par
private static final long serialVersionUID = 1L;\par
\par
private Image image;\par
private BufferedImage buf;\par
\par
private ImagePanel ip;\par
private MainSolution main;\par
private JTextField filenameBox;\par
private JButton btnLoadImage, btnZoom;\par
private String currentPath = "C:\\\\";\par
\par
public ControlPanel(MainSolution main)\{\par
super(new FlowLayout());\par
this.main = main;\par
this.ip = main.getImagePanel();\par
\par
initPanel();\par
\}\par
\par
private void initPanel() \{\par
\par
btnLoadImage = new JButton("Load Image...");\par
btnLoadImage.addActionListener(new ControlPanel_btnLoadImage_actionAdapter(this));\par
add(btnLoadImage);\par
\par
filenameBox = new JTextField(50);\par
add(filenameBox);\par
\par
btnZoom = new JButton("Zoom in");\par
btnZoom.addActionListener(new ControlPanel_btnZoom_actionAdapter(this));\par
add(btnZoom);\par
\par
\}\par
\par
public void btnLoadImage_actionPerformed(ActionEvent e) \{\par
//TODO load image\par
try \{\par
JFileChooser jfc = new JFileChooser(currentPath);\par
jfc.showOpenDialog(null);\par
File f = jfc.getSelectedFile();\par
if(f != null)\{\par
currentPath = f.getCanonicalPath();\par
String filename = f.getName();\par
filenameBox.setText(currentPath);\par
ip.loadImage(currentPath);\par
\par
main.setTitle(main.getTitle()+ ": " + filename);\par
\par
//khoi tao cac doi tuong can thiet\par
image = loadImage(currentPath);\par
buf = ImageIO.read(f);\par
\}\par
\} catch (IOException ioe)\{\par
JOptionPane.showMessageDialog(main,\par
"File not found", "Error", JOptionPane.ERROR_MESSAGE);\par
\}\par
\par
\}\par
\par
public void btnZoom_actionPerformed(ActionEvent e) \{\par
\par
zoom(buf, 2);\par
System.out.println("Result: " +buf.getWidth() +" " + buf.getHeight());\par
ip.repaint();\par
\}\par
\par
public void setDefaultFilename(String filename)\{\par
filenameBox.setText(filename);\par
filenameBox.selectAll();\par
\}\par
private Image loadImage(String path) \{\par
try \{\par
Image img = Toolkit.getDefaultToolkit().getImage(path);\par
MediaTracker mt = new MediaTracker(this);\par
mt.addImage(img, 0);\par
mt.waitForAll();\par
return img;\par
\}\par
catch (Exception ex) \{\par
return null;\par
\}\par
\}\par
public BufferedImage zoom(BufferedImage bi, int scale)\par
\{\par
int width = scale * bi.getWidth();\par
int height = scale * bi.getHeight();\par
\par
BufferedImage biScale = new BufferedImage(width, height, bi.getType());\par
\par
for(int i=0; i<width; i++)\par
for(int j=0; j<height; j++)\par
biScale.setRGB(i, j, bi.getRGB(i/scale, j/scale));\par
System.out.println(biScale.getWidth() +" " + biScale.getHeight());\par
\par
return biScale;\par
\}\par
\}\par
\par
class ControlPanel_btnLoadImage_actionAdapter implements ActionListener \{\par
private ControlPanel adaptee;\par
ControlPanel_btnLoadImage_actionAdapter(ControlPan el adaptee) \{\par
this.adaptee = adaptee;\par
\}\par
\par
public void actionPerformed(ActionEvent e) \{\par
adaptee.btnLoadImage_actionPerformed(e);\par
\}\par
\}\par
\par
class ControlPanel_btnZoom_actionAdapter implements ActionListener \{\par
private ControlPanel adaptee;\par
ControlPanel_btnZoom_actionAdapter(ControlPanel adaptee) \{\par
this.adaptee = adaptee;\par
\}\par
\par
public void actionPerformed(ActionEvent e) \{\par
adaptee.btnZoom_actionPerformed(e);\par
\}\par
\}\par
\par
\par
\par
class ImagePanel\par
\par
import java.awt.*;\par
\par
import javax.swing.JPanel;\par
\par
public class ImagePanel extends JPanel \{\par
\par
private static final long serialVersionUID = 1L;\par
\par
private BufferedImage buf;\par
private MainSolution main;\par
\par
public ImagePanel(MainSolution main)\{\par
buf = null;\par
this.main = main;\par
\}\par
\par
public void loadImage(String filename)\{\par
// get the image from filename first\par
Image img = Toolkit.getDefaultToolkit().getImage(filename);\par
\par
try\{\par
MediaTracker tracker = new MediaTracker(this);\par
tracker.addImage(img, 0);\par
tracker.waitForID(0);\par
\} catch (Exception e)\{\}\par
\par
int width = img.getWidth(this);\par
int height = img.getHeight(this);\par
\par
buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);\par
Graphics2D biContext = buf.createGraphics();\par
biContext.drawImage(img, 0, 0, null);\par
\par
// this one from the JPanel\par
setPreferredSize(new Dimension(buf.getWidth(), buf.getHeight()));\par
System.out.println(width + " " + height);\par
\par
revalidate();\par
repaint();\par
\}\par
\par
\par
public void paint(Graphics g)\{\par
super.paint(g);\par
Graphics2D g2d = (Graphics2D) g;\par
if(buf != null)\{\par
g2d.drawImage(buf, 50, 0, this);\par
\}\par
\}\par
\par
\par
\}\par
verbway\par
29-05-2008, 14:27\par
Nh\'ecn qua th\'ec th\u7845?y b\u7841?n g\u7885?i h\'e0m zoom nh\f2\'fdng kh\'f4ng s\u7917? d\u7909?ng gi\'e1 tr\u7883? tr\u7843? v\u7873?. Th\u7917? ch\u7919?a nh\'fd sau xem (nx: ch\'fd\'f5ng tr\f1\'ecnh c\'f3 logic h\f2\'f5i... l\u7857?ng nh\u7857?ng qu\'e1):\par
\par
public void btnZoom_actionPerformed(ActionEvent e) \{\par
\par
buf = zoom(buf, 2);\par
System.out.println("Result: " +buf.getWidth() +" " + buf.getHeight());\par
ip.repaint();\par
\}\par
\f1 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\par
\par
}
 